package com.idroid.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class RecordProgressBar extends View {
    private Paint paint;
    private RectF mProgressCircle;
    private int roundColor;
    private int roundProgressColor;
    private float roundWidth;
    private float max;
    private float progress;

    public RecordProgressBar(Context context) {
        this(context, null);
    }

    public RecordProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RecordProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        paint = new Paint();
        roundColor = Color.parseColor("#ffd4b5");
        roundProgressColor = Color.WHITE;
        roundWidth = 20;
        max = 60f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画圆环
        int centre = getWidth() / 2; //获取圆心的x坐标
        int radius = (int) (centre - roundWidth / 2);
        paint.setColor(roundColor);
        paint.setAlpha(128); // alpha值的范围是0~255
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(roundWidth);
        paint.setAntiAlias(true);
        canvas.drawCircle(centre, centre, radius, paint); //画出圆环


        //画进度
        paint.setAlpha(255);
        paint.setStrokeWidth(roundWidth);
        paint.setColor(roundProgressColor);
        if (null == mProgressCircle) {
            mProgressCircle = new RectF(centre - radius, centre - radius, centre
                    + radius, centre + radius);
        }

        canvas.drawArc(mProgressCircle, 270, 360 * progress / max, false, paint);

    }

    public void setProgress(float progress) {
        if (progress < 0) {
            throw new IllegalArgumentException("progress not less than 0");
        }
        if (progress > max) {
            progress = max;
        }
        if (progress <= max) {
            this.progress = progress;
            invalidate();
        }
    }
}